import {getAreaConfig, getIndustryConfig, getTagTypeConfig} from '@/utils/filterConfig';
import {getBondType, getCaliberType, getFinancialType, getVarietyType} from './../apis';
import {ComponentType} from 'vc';
import {useRequest} from 'vue-request';

export function useScreen() {
  const PAGE_SIZE = 15;

  const queryData = reactive<any>({
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    // sort: 'desc',
    bondCompanyType: 1
  });

  const option1 = [
    {name: '不限', code: ''},
    {name: '是', code: 2},
    {name: '否', code: 1}
  ];
  const option2 = [
    {name: '不限', code: ''},
    {name: '有', code: 1},
    {name: '无', code: 2}
  ];

  const moreData = [
    {
      name: '是否有第三方评估认证',
      code: 'hasFacilityRating',
      data: option2
    },
    {
      name: '是否到期',
      code: 'maturityType',
      data: option1
    },
  ];

  // 地区
  const areaData = getAreaConfig();
  areaData.value.submitFn = (val) => {
    const {lv1, lv2, lv3} = val;

    queryData.provinceCode = lv1.join();
    queryData.cityCode = lv2.join();
    queryData.countryCode = lv3.join();
  };

  // 绿债口径分类
  const {data: caliberData} = useRequest(getCaliberType, {});
  const caliberTypeData = {
    title: '绿债口径分类',
    component: ComponentType.SINGLE,
    options: caliberData,
    required: false,
    submitFn: (data) => {
      queryData.greenBondCaliber = data;
    }
  };

  // 绿债品种分类
  const {data: varietyData} = useRequest(getVarietyType, {});
  const varietyTypeData = {
    title: '绿债品种分类',
    component: ComponentType.SINGLE,
    options: varietyData,
    required: false,
    submitFn: (data) => {
      queryData.greenBondVariety = data;
    }
  };


  // 债券类型
  const {data} = useRequest(getBondType, {});
  const classTypeData = {
    title: '债券类型',
    component: ComponentType.MULTIPLE,
    options: data,
    submitFn: (data) => {
      queryData.bondType = data;
    }
  };

  const bondTypeDataMoreOption = [
    {
      name: '绿债口径分类',
      code: 'greenBondCaliber',
      data: caliberData,
      isNoLimit: true
    },
    {
      name: '绿债品种分类',
      code: 'greenBondVariety',
      data: varietyData,
      isNoLimit: true
    },
    {
      name: '债券类型',
      code: 'bondType',
      data: data,
      multiple: true,
      isNoLimit: true
    },
  ];


  // 金融机构类型
  const {data: finanTpyeData} = useRequest(getFinancialType, {});
  const financialTypeData = {
    title: '机构类型',
    component: ComponentType.CASCADER,
    options: finanTpyeData,
    submitFn: (data) => {
      const {lv1, lv2, lv3} = data
      queryData.companyType = lv2.join(',');
    }
  };

  // 更多
  const moreFilter = ref({
    title: '更多',
    component: 'tiled-select',
    options: moreData,
    submitFn: (data) => {
      Object.assign(queryData, data);
    }
  });

  // 分类
  const moreBondTypeFilter = ref({
    title: '分类',
    component: 'tiled-select',
    options: bondTypeDataMoreOption,
    submitFn: (data) => {
      Object.assign(queryData, data);
    }
  });

  // 发行日期
  const dateData = {
    title: '发行日期',
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.issuingDateStart = data[0];
      queryData.issuingDateEnd = data[1];
    }
  };

  const screen = ref([areaData.value, moreBondTypeFilter.value, moreFilter.value, dateData
  ]);
  const screen2 = ref([moreBondTypeFilter.value, financialTypeData, moreFilter.value, dateData
  ]);

  return {
    PAGE_SIZE,
    screen,
    screen2,
    queryData
  };
}
